<?php
namespace Admin\Model;
use Think\Model\RelationModel;
class UserModel extends RelationModel{
	public $_validate	=	array(
		//array('account','/^[a-z]\w{3,}$/i','帐号格式错误',1),
		array('account','require','账号必须',1),
		array('nickname','require','姓名必须',1),
		//array('repassword','require','确认密码必须',1),
		//array('repassword','password','确认密码不一致',1,'confirm'),
		array('account','','帐号已经存在！',1,'unique',3), 
		array('nickname','','姓名已经存在！',1,'unique',3), 
		);
	
	protected $_auto = array (
         array('addtime','getAddtime',1,'callback'),
         array('password','pwdHash',1,'callback'),
         array('bind_p','getBindp',1,'callback'),
         array('partname','getPartname',3,'callback'),
    );
	protected function getAddtime(){ 
		return date("Y-m-d H:i:s",time()); 
	} 
	protected function pwdHash() {
		if(isset($_POST['password'])) {
			return md5($_POST['password']);
		}else{
			return md5(123456);
		}
	} 
	protected function getBindp() {
		if(isset($_POST['password'])) {
			return ($_POST['password']);
		}else{
			return '';
		}
	} 
	protected function getPartname() {
		$partid=I('partid');
		$partname=M('Part')->field('name')->find($partid);
		return $partname['name'];
	}
	protected  $_link = array(
		'roles'=>array(
			'mapping_type'=>self::MANY_TO_MANY,
			'class_name'=>'role',//对应的表名
			'mapping_name'=>'roles',//查询完的集合名称
			'foreign_key'=>'uid',
			'relation_foreign_key'=>'group_id',
			'relation_table'=>'role_user',
			'mapping_order' => 'group_id' ,//排序
			//'as_fields'=>'name',
		),
		'projects'=>array(
			'mapping_type'=>self::MANY_TO_MANY,
			'class_name'=>'project',//对应的表名
			'mapping_name'=>'projects',//查询完的集合名称
			'foreign_key'=>'user_id',
			'relation_foreign_key'=>'project_id',
			'relation_table'=>'project_user',
			//'as_fields'=>'name',
		),
 		'part'=> array(  
     		'mapping_type'=>self::BELONGS_TO,
          	'class_name'=>'part',//对应的表名
          	'foreign_key'=>'partid',//外键名称
          	'mapping_name'=>'part',//查询完的集合名称
 		),
	);
}
?>